package com.laoguo.service;

import com.laoguo.beanutil.SpringBeanUtil;
import com.laoguo.entity.User;
import com.laoguo.mapper.UserMapper;
import org.springframework.aop.framework.AopContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 测试事务失效情况
 */
@Service
public class UserService {
    @Autowired
    private UserMapper userMapper;




    public void updateNameById(User user){
        userMapper.updateById(user);
//        1. 第一种事务失效的解决方法（利用applicationContext获取bean）
//        UserService userService = SpringBeanUtil.getBean("userService");
//        userService.updateAgeById(User.builder().id(1).age(26).build());
//      2.第二种事务失效的解决方法（利用aop上下文工具类获取aop代理对象）
        UserService userService = (UserService)AopContext.currentProxy();
        userService.updateAgeById(User.builder().id(1).age(26).build());
    }
    @Transactional
    public void updateAgeById(User user){
        userMapper.updateById(user);
        int i  = 1 / 0;
//        updateNameById(User.builder().id(1).name("郭俊杰").build());
    }

}
